package com.synjones.handsetS8.ild.net.api;

import com.google.gson.Gson;
import com.synjones.handsetS8.ild.constants.Headers;
import com.synjones.handsetS8.ild.constants.HttpDefault;
import com.synjones.handsetS8.ild.net.common.HttpUtils;
import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetClient;
import com.synjones.handsetS8.ild.net.model.ListResult;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.net.model.WorkerResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;

/**
 * 花名册 接口
 */
public class RosterApi {
    private static final String BASE_URL = "http://jiangong-manager.dev1.i-laodong.com/";
    private static IdCardService service = NetClient.getRetrofit(BASE_URL).create(IdCardService.class);

    /**
     * 获取分包公司列表
     *
     * @param realName
     * @param teamId
     * @param startTime
     * @param endTime
     * @param page
     * @param size
     * @param callback
     */
    public static void getWorkerList(String realName, Long teamId, Long startTime, Long endTime, long page, long size, NetCallback<NetResult<ListResult<WorkerResult>>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("realname", realName);
        map.put("team_id", teamId);
        map.put("created_at_start", startTime);
        map.put("created_at_end", endTime);
        map.put("page", page);
        map.put("size", size);
        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(map));

        Call<NetResult<ListResult<WorkerResult>>> call = service.getWorkerList(HttpUtils.getAuthKey(), requestBody);
        call.enqueue(callback);
    }

    /**
     * 更换手环
     *
     * @param workerId
     * @param braceletNo
     * @param callback
     */
    public static void changeBracelet(String workerId, String braceletNo, NetCallback<NetResult> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("worker_id", workerId);
        map.put("bracelet_coding", braceletNo);
        System.out.println(new Gson().toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(map));

        Call<NetResult> call = service.changeBracelet(HttpUtils.getAuthKey(), requestBody);
        call.enqueue(callback);
    }

    /**
     * 退工
     *
     * @param workerId
     * @param braceletStatus
     * @param callback
     */
    public static void retire(String workerId, int braceletStatus, NetCallback<NetResult> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("worker_id", workerId);
        map.put("bracelet_coding_status", braceletStatus);
        System.out.println(new Gson().toJson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(map));

        Call<NetResult> call = service.retire(HttpUtils.getAuthKey(), requestBody);
        call.enqueue(callback);
    }

    interface IdCardService {
        /**
         * 获取花名册工人列表
         *
         * @return
         */
        @POST("api/handheld-worker/list")
        Call<NetResult<ListResult<WorkerResult>>> getWorkerList(@Header(Headers.AUTH_KEY) String authKey, @Body RequestBody body);

        /**
         * 更换手环
         *
         * @return
         */
        @POST("api/worker/date-update")
        Call<NetResult> changeBracelet(@Header(Headers.AUTH_KEY) String authKey, @Body RequestBody body);

        /**
         * 退工
         *
         * @return
         */
        @POST("api/worker/retire")
        Call<NetResult> retire(@Header(Headers.AUTH_KEY) String authKey, @Body RequestBody body);
    }
}
